package com.sh.caryr.erp.excel;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.sh.caryr.erp.entity.basis.ExpressComp;
import com.sh.caryr.erp.entity.product.ProductSpec;
import com.sh.caryr.erp.entity.trade.Order;
import com.sh.caryr.erp.entity.trade.TradeOrder;
import com.sh.caryr.erp.util.UtilityTool;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by milk on 2018/2/1.
 */
public class ExportToExcel {
    public static String outputFolderPath = "./test/export";

    public static void exportProductSpec(List<ProductSpec> productSpecs) throws Exception{
        ExportParams params = new ExportParams(null, "abc", ExcelType.XSSF);
        Workbook workbook = ExcelExportUtil.exportExcel(params,
                ProductSpec.class, productSpecs);
        //workbook.cloneSheet(1);

        File dir = new File(outputFolderPath);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        FileOutputStream fos = new FileOutputStream(outputFolderPath+"/productSpecs.xlsx");
        workbook.write(fos);

        fos.close();

    }

    public static void exportTemplates(List<Order> orderList, List<ProductSpec> productSpecs, String origFile,
                                       String destDir, String destFileName) throws Exception{
        //读取模版
        TemplateExportParams params = new TemplateExportParams(origFile,true);

        //准备数据
        Map<String, Object> map = new HashMap<>();

        //准备数据 订单数据
        //由于eazypoi不支持嵌套循环，所以将嵌套循环改为单循环！！！
        List<Map<String, Object>> tradeOrderMapList = new ArrayList<>();
        for(Order order : orderList){
            boolean isPut = false;
            if (order != null && order.getTradeOrderList() != null) {
                for(TradeOrder tradeOrder: order.getTradeOrderList()){
                    Map<String, Object> lm = new HashMap<>();
                    if(!isPut) {
                        lm.put("trade", order);
                        isPut = true;
                    }
                    //规格id 加x
                    lm.put("productSpecId", tradeOrder.getProductSpecId() + "x");
                    lm.put("tradeOrder", tradeOrder);
                    tradeOrderMapList.add(lm);
                }
            }
        }
        map.put("tradeOrderMapList", tradeOrderMapList);

        //准备数据 规格信息
        List<Map<String, String>> maplist = new ArrayList<>();
        for(ProductSpec productSpec : productSpecs){
            Map<String, String> lm = new HashMap<>();
            lm.put("name", productSpec.getName());
            lm.put("id", productSpec.getId() + "x");
            maplist.add(lm);
        }
        map.put("maplist", maplist);

        //准备数据 快递信息
        List<Map<String, String>> maplist2 = new ArrayList<>();
        for (ExpressComp expressComp : UtilityTool.getExpressComps()){
            Map<String, String> lm = new HashMap<>();
            lm.put("expressName", expressComp.getName());
            lm.put("expressCode", expressComp.getCode());
            maplist2.add(lm);
        }
        map.put("maplist2", maplist2);

        //sheet 2 无用 写入规格信息
        //params.setSheetNum(1);
        Workbook book = ExcelExportUtil.exportExcel(params, map);

        File savefile = new File(destDir);
        if (!savefile.exists()) {
            savefile.mkdirs();
        }
        FileOutputStream fos = new FileOutputStream(destDir + File.separator + destFileName);
        book.write(fos);
        fos.close();

    }

    public static void exportTrades(List<Order> productSpecs, String destDir,
                                    String destFileName) throws Exception{
        ExportParams params = new ExportParams("订单详细信息", "订单详细信息", ExcelType.XSSF);

        Workbook workbook = ExcelExportUtil.exportExcel(params, Order.class, productSpecs);

        File savefile = new File(destDir);
        if (!savefile.exists()) {
            savefile.mkdirs();
        }
        FileOutputStream fos = new FileOutputStream(destDir + File.separator + destFileName);
        workbook.write(fos);
        fos.close();
    }
}
